﻿/**
*    Copyright (C) 2008  Chase Kernan 
*    chase.kernan@gmail.com
*
*    This program is free software: you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
* 
*/

package com.chasekernan.hxnova.core.dataholders;

import com.chasekernan.hxnova.core.fleets.Salvage;

/**
    Contains all of the salvage in the known universe.
**/
class SalvageHolder extends ParticleHolder<Salvage>{

    public function new() {
        super();
    }
    
    public static var XML_TITLE = "salvage_data";
    
    /**
        TODO: insert sample xml.
    **/
    public static function writeToXml(sh : SalvageHolder) : Xml {
        var xml = Xml.parse('<' + XML_TITLE + '></' + XML_TITLE + '>')
                .firstChild();
        
        xml.set('num_salvage', Std.string(sh.particles.length));
        
        for (salvage in sh) {
            if (salvage == null) continue;
            xml.addChild(Salvage.writeToXml(salvage));
            xml.addChild(Xml.createElement("spacer"));
        }
        
        return xml;
    }
    
    public static function readFromXml(xml : Xml) : SalvageHolder {
        try {
            var sh = new SalvageHolder();
            sh.expand(Std.parseInt(xml.get('num_salvage')));
            
            for (e in xml.elementsNamed(XML_TITLE)) {
                sh.addParticle(Salvage.readFromXml(e));
            }
            
            return sh;
        } catch (e : Dynamic) {
            throw "Error while reading from salvage holder xml: " + e;
        }
    }
    
}